package com.qidian.mes.dv.service.Impl;

import com.qidian.mes.dv.mapper.MachineryMapper;
import com.qidian.mes.dv.service.MachineryService;
import com.qidian.mes.pojo.Machinery;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author 陆雯城
 * @Description
 * @Time 2024-04-14 14:55
 */
@Transactional
@Service
public class MachineryServiceImpl implements MachineryService {
    @Autowired
    private MachineryMapper machineryMapper;

    @Override
    public QdResult deleteByMachineryIds(Long[] machineryIds) {
        machineryMapper.deleteByMachineryIds(machineryIds);
        return QdResult.success();
    }

    @Override
    public QdResult deleteByMachineryId(Long machineryId) {
        machineryMapper.deleteByMachineryId(machineryId);
        return QdResult.success();
    }

    @Override
    public QdResult updateMachinery(Machinery machinery) {
        Machinery tempMachinery = machineryMapper.selectByMachineryName(machinery.getMachineryName());
        if (tempMachinery != null && !tempMachinery.getMachineryId().equals(machinery.getMachineryId())) {
            return QdResult.duplicate("duplicate: machineryName");
        }
        tempMachinery = machineryMapper.selectByMachineryCode(machinery.getMachineryCode());
        if (tempMachinery != null && !tempMachinery.getMachineryId().equals(machinery.getMachineryId())) {
            return QdResult.duplicate("duplicate: machineryCode");
        }
        return QdResult.isSuccess(machineryMapper.updateMachinery(machinery));
    }

    @Override
    public QdResult queryByMachineryId(Long machineryId) {
        return QdResult.success(machineryMapper.selectByMachineryId(machineryId));
    }

    @Override
    public QdResult saveMachinery(Machinery machinery) {
        if (machineryMapper.selectByMachineryName(machinery.getMachineryName()) != null) {
            return QdResult.duplicate("duplicate: machineryName");
        }
        if (machineryMapper.selectByMachineryCode(machinery.getMachineryCode()) != null) {
            return QdResult.duplicate("duplicate: machineryCode");
        }
        return QdResult.isSuccess(machineryMapper.insertMachinery(machinery));
    }

    @Override
    public QdResult<PagerDTO<Machinery>> queryByPage(Machinery machinery, int page, int limit) {
        PagerDTO<Machinery> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(page);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(machineryMapper.selectCount(machinery));
        pagerDTO.setData(machineryMapper.queryByPage(machinery, (page - 1) * limit, limit));
        return QdResult.success(pagerDTO);
    }
}
